package org.programmers.anonymous.fightrobots.dao.impl;

import java.util.List;

import org.programmers.anonymous.fightrobots.dao.IRobotDao;
import org.programmers.anonymous.fightrobots.model.IRobot;
import org.programmers.anonymous.fightrobots.model.Robot;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Implements the IRobotDao Interface
 * 
 * @author Daniel Cruz, Francisco Villalba
 *
 */
public class RobotDao extends HibernateDaoSupport implements IRobotDao {

	@Override
	public int save(IRobot r) 
	{
		Integer id = null;
		if (Integer.valueOf(r.getIdRobot()) == -99)
		{

			id = (Integer) getHibernateTemplate().save(r);
			r.setIdRobot(id);

		}
		else
		{
			getHibernateTemplate().saveOrUpdate(r);
			id = r.getIdRobot();
		}
		return id;
	}
	
	@Override
	public IRobot loadRobot(int Id) 
	{
		return  getHibernateTemplate().get(Robot.class, Id);

	}

	@Override
	public void delete(IRobot r) 
	{
		getHibernateTemplate().delete(r);
	}

	@SuppressWarnings("unchecked")
	@Override

	public List<IRobot> getRobotList() 
	{
		return  getHibernateTemplate().find("from Robot");
	}

	@Override
	public void deleteAllRobots() 
	{
		for (IRobot robot : getRobotList())
		{
			delete(robot);
		}
	}

}
